<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="真实姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户微信昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换时间" prop="rowTime">
        <el-date-picker clearable
                        v-model="queryParams.rowTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择用户兑换时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning"  icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['applet:appletWalkRow:export']">导出</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['applet:appletWalkRow:export']">导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="appletWalkRowList" border>
      <el-table-column label="记录ID" align="center" prop="rowId" v-if="false"/>
      <el-table-column label="用户ID" align="center" prop="userId" v-if="false"/>
      <el-table-column label="微信昵称" width="150" show-overflow-tooltip align="center" prop="nickName"/>
      <el-table-column label="真实姓名" width="150" show-overflow-tooltip align="center" prop="userName"/>
      <el-table-column label="微信头像" align="center" prop="avatarUrl" width="150">
        <template slot-scope="scope">
          <el-avatar :size="50" :src="scope.row.avatarUrl"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="兑换日期" align="center" prop="rowTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rowTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信记录步数" align="center" width="150" prop="nowWalk">
        <template slot-scope="scope">
          <span>{{ scope.row.nowWalk }} 步</span>
        </template>
      </el-table-column>
      <el-table-column label="已兑换步数" align="center" width="150" prop="agoWalk">
        <template slot-scope="scope">
          <span>{{ scope.row.agoWalk }} 步</span>
        </template>
      </el-table-column>
      <el-table-column label="已兑换里程" align="center" width="150" prop="agoWalk">
        <template slot-scope="scope">
          <span>{{ (scope.row.agoWalk / 20).toFixed(2) }} 里</span>
        </template>
      </el-table-column>
      <el-table-column label="可兑换步数" align="center" width="150" prop="agoWalk">
        <template slot-scope="scope">
          <span>{{ scope.row.nowWalk - scope.row.agoWalk }} 步</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:appletWalkRow:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:appletWalkRow:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改appletWalkRow对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :sm="24" :lg="12">
            <el-form-item label="微信昵称:" prop="nickName">
              <el-input v-model="form.nickName" disabled placeholder="请输入用户昵称"/>
            </el-form-item>
            <el-form-item label="真实姓名:" prop="userName">
              <el-input v-model="form.userName" disabled placeholder="请输入真实姓名"/>
            </el-form-item>
            <el-form-item label="已兑换步数:" prop="agoWalk">
              <el-input-number v-model="form.agoWalk" disabled placeholder="请输入兑换前步数"/>
            </el-form-item>
            <el-form-item label="微信步数:" prop="nowWalk">
              <el-input-number v-model="form.nowWalk" disabled placeholder="请输入当天微信步数"/>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :lg="12">
            <el-form-item label="用户头像:" prop="avatarUrl">
              <image-upload v-model="form.avatarUrl" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注:" prop="remark">
          <el-input type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppletWalkRow,
  getAppletWalkRow,
  delAppletWalkRow,
  addAppletWalkRow,
  updateAppletWalkRow
} from "@/api/applet/appletWalkRow";

export default {
  name: "AppletWalkRow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // appletWalkRow表格数据
      appletWalkRowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        nickName: null,
        avatarUrl: null,
        rowTime: null,
        userName: null,
        agoWalk: null,
        nowWalk: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        nickName: [
          {required: true, message: "用户昵称不能为空", trigger: "blur"}
        ],
        avatarUrl: [
          {required: true, message: "用户头像不能为空", trigger: "blur"}
        ],
        rowTime: [
          {required: true, message: "记录时间不能为空", trigger: "blur"}
        ],
        userName: [
          {required: true, message: "真实姓名不能为空", trigger: "blur"}
        ],
        agoWalk: [
          {required: true, message: "兑换前步数不能为空", trigger: "blur"}
        ],
        nowWalk: [
          {required: true, message: "当天微信步数不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        createBy: [
          {required: true, message: "创建账号不能为空", trigger: "blur"}
        ],
        updateBy: [
          {required: true, message: "更新账号不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询appletWalkRow列表 */
    getList() {
      this.loading = true;
      listAppletWalkRow(this.queryParams).then(response => {
        this.appletWalkRowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowId: null,
        userId: null,
        nickName: null,
        avatarUrl: null,
        rowTime: null,
        userName: null,
        agoWalk: null,
        nowWalk: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rowId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加appletWalkRow";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rowId = row.rowId || this.ids
      getAppletWalkRow(rowId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户兑换记录信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rowId != null) {
            updateAppletWalkRow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletWalkRow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rowIds = row.rowId || this.ids;
      this.$modal.confirm('是否确认删除appletWalkRow编号为"' + rowIds + '"的数据项？').then(function () {
        return delAppletWalkRow(rowIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/appletWalkRow/export', {
        ...this.queryParams
      }, `appletWalkRow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
